<template>
  <div>
      <!-- 头部搜索框 -->
    <div class="search-header">
      <!-- 后退按钮 -->
      <van-icon name="arrow-left" color="white" size="0.48rem" class="goback"
       @click="$router.back(-1)"/>
      <!-- 搜索组件 -->
      <van-search placeholder="请输入搜索关键词" background="#007BFF" shape="round"
      v-model="kw"
      @input="changekw"
      @select="addhis"
      v-iptfocus/>
    </div>
    <!-- 渲染页面 -->
    <div class="sugg-list" v-if="kw.trim().length>0?true:false">
    <div class="sugg-item"
     v-for="(item,index) in suggestlist"
     :key="index"
     v-html="getlight(item)"
     @click="clickaddhis(item)">

    </div>
</div>
<!-- 搜索历史 -->
    <div class="search-history" v-else>
      <!-- 标题 -->
      <van-cell title="搜索历史">
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
        <template #right-icon>
          <van-icon name="delete" class="search-icon" @click="delallhis"/>
        </template>
      </van-cell>

      <!-- 历史列表 -->
      <div class="history-list">
        <span class="history-item"
        v-for="(str,index) in history" :key="index" @click="addstrhis(str)">{{str}}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { suggestAPI } from '@/api'
import { setStorage, getStorage, removeStorage } from '@/utils/Storage.js'
export default {
  data () {
    return {
      kw: '', // 关键字 v-model双向绑定
      suggestlist: [], // 返回的搜索结果
      timer: null,
      history: JSON.parse(getStorage('his')) || []
    }
  },
  methods: {
    // 搜索菜单防抖
    changekw () {
      clearTimeout(this.timer)
      this.timer = setTimeout(async () => {
        if (this.kw.length > 0) {
          const res = await suggestAPI({ q: this.kw })
          this.suggestlist = res.data.data.options
        }
      }, 1000)
    },
    // 关键字高亮
    getlight (str) {
      this.kw = this.kw.trim()
      const reg = new RegExp(this.kw, 'ig')
      return str.replace(reg, (match) => {
        return `<span style="color:red;">${match}</span>`
      })
    },
    // 当输入搜索菜单,输入框内的关键字变成搜索历史
    addhis () {
      this.kw = this.kw.trim()
      this.history.push(this.kw)
      this.history = Array.from(new Set(this.history))
      localStorage.setItem('his', JSON.stringify(this.history))
    },
    // 按回车跳转搜索结果页面
    clickaddhis (item) {
      // 存到历史记录
      this.history.push(item)
      this.history = Array.from(new Set(this.history))
      setStorage('his', JSON.stringify(this.history))
      //   点击搜索提示菜单,跳转到搜索结果页面
      this.$router.push({
        path: `/search/${item}`
      })
      this.kw = ''
    },
    // 点击历史记录,回填到input 进行查找 跳转搜索结果页面
    addstrhis (str) {
      this.kw = str
      this.kw = this.kw.trim()
      this.changekw()
      this.$router.push({
        path: `/search/${str}`
      })
    },
    delallhis () {
      this.history = []
      removeStorage('his')
    }
  }
}
</script>

<style scoped lang="less">
.search-header {
  height: 46px;
  display: flex;
  align-items: center;
  background-color: #007bff;
  overflow: hidden;
  /*后退按钮*/
  .goback {
    padding-left: 14px;
  }
  /*搜索组件*/
  .van-search {
    flex: 1;
  }

}
.sugg-list {
  .sugg-item {
    padding: 0 15px;
    border-bottom: 1px solid #f8f8f8;
    font-size: 14px;
    line-height: 50px;
    // 实现省略号的三行代码
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
/**搜索历史 */
.search-icon {
  font-size: 16px;
  line-height: inherit;
}

.history-list {
  padding: 0 10px;
  .history-item {
    display: inline-block;
    font-size: 12px;
    padding: 8px 14px;
    background-color: #efefef;
    margin: 10px 8px 0px 8px;
    border-radius: 10px;
  }
}
</style>>
